﻿namespace KinonekoSoftware.CodariaDB.Documents.Index
{
    public class TeamCacheEngine : _Delta<TeamCache>
    {

        //
        //    @ _Cache
        //    用于实现SpeciesCache的缓存，这有助于提高速度、降低内存占用
        //    并且更方便的实现，可观测对象的内存一致性
        private readonly Dictionary<string, TeamCache> _Cache      = new Dictionary<string, TeamCache>();

        //-------------------------------------------------------------
        //
        //          Override Methods
        //
        //-------------------------------------------------------------

        public override string GetCollectionName() => CollectionNames.Cache_TeamDB;


        protected internal override void OnDatabaseClosingInternal(ILogger logger)
        {
            base.OnDatabaseClosingInternal(logger);

            _Cache.Clear();
        }


        //-------------------------------------------------------------
        //
        //          Public Methods
        //
        //-------------------------------------------------------------

        public void AddCache(TeamCache cache)
        {
            if (cache is null)
            {
                return;
            }

            if (_Cache.ContainsKey(cache.Id))
            {
                InsideDB.Update(cache);
            }
            else
            {
                _Cache.TryAdd(cache.Id, cache);
                InsideDB.Insert(cache);
            }

            Modified(cache);

        }

        public void UpdateCache(TeamCache cache)
        {
            if (cache is null)
            {
                return;
            }

            if (!_Cache.ContainsKey(cache.Id))
            {
                _Cache.TryAdd(cache.Id, cache);
            }

            InsideDB.Update(cache);

            Modified();
        }

        public void RemoveCache(TeamCache cache, bool softDelete = true)
        {
            if (cache is null)
            {
                return;
            }

            if (softDelete)
            {
                cache.IsDeleted = true;
                InsideDB.Update(cache);

            }
            else
            {
                InsideDB.Delete(cache.Id);

            }
            var id = cache.Id;

            _Cache.Remove(id, out _);
            Modified();
        }

        public bool Has(string id) => InsideDB.HasID(id);


        #region Get

        /// <summary>
        /// 缓存命中的Get策略
        /// </summary>
        /// <param name="id">设定的ID</param>
        /// <returns></returns>
        public TeamCache Get(string id)
        {
            if (_Cache.TryGetValue(id, out var cache))
            {
                return cache;
            }

            cache = InsideDB.FindById(id);
            _Cache.TryAdd(id, cache);
            return cache;
        }


        /// <summary>
        /// 带缓存策略的Get
        /// </summary>
        /// <returns></returns>
        public IEnumerable<TeamCache> Get() => _Cache.Values;

        #endregion


        public override string EngineID => EngineNames.Cache_Region;
    }
}